//
//  MoodContentViewMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-3-10.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "MoodContentViewMaker.h"
#import "Config.h"
#import "DynamicPicturesView.h"
#import "DynamicFooterView.h"
#import "RTLabel.h"

@implementation MoodContentViewMaker

+ (void)makePrototype:(UIView *)view
{
    if (view) {
        //文字部分
        UITextView* contentView = [[UITextView alloc] initWithFrame:CGRectMake(10, 5, 290, 20)];
        contentView.font = _SYS_FONT_002;
        contentView.backgroundColor = [UIColor clearColor];
        contentView.editable = false;
        [view addSubview:contentView];

        contentView.contentInset = UIEdgeInsetsMake(-8, -8, 0, 0);
        if (iOS7) {
            contentView.contentInset = UIEdgeInsetsMake(-8, -5, 0, 0);
        }
        
        //图片视图部分
        DynamicPicturesView *picturesView = [[DynamicPicturesView alloc] initWithFrame:CGRectMake(10, contentView.frame.origin.y + contentView.frame.size.height + 5, 280, 0)];
        picturesView.hidden = true;
        picturesView.backgroundColor = [UIColor clearColor];
        [view addSubview:picturesView];
        
        
        //统计
        RTLabel* footerLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, picturesView.frame.origin.y + picturesView.frame.size.height + 10, 280, 18)];
        footerLabel.font = _SYS_FONT_002;
        [view addSubview:footerLabel];
        
        //footer视图部分
//        UIView *footerView = [[DynamicFooterView alloc] initWithFrame:CGRectMake(5, picturesView.frame.origin.y + picturesView.frame.size.height + 10, 280, 18) forStyle:FooterViewStyleSigle];
//        footerView.backgroundColor = [UIColor clearColor];
//        [view addSubview:footerView];
    }
}


// 计算一个动态的高度
+ (float)heightWithDictionary:(NSDictionary *)data
{
    float height = 0.0f;
    
    if (data == nil || [data isKindOfClass:[NSNull class]]) {
        return height;
    }
    
    //文字内容
    NSString *content = data[@"content"];
    
    //根据content计算，此消息记录对应的label的size，这里的font和绘制cell时的font一定要一样
    if ([content length] != 0) {
        
        CGSize size = [content sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(280.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += size.height;
    }

    
    //图片
    NSArray *pictures = data[@"pictures"];
    
    //动态要有图片 且 图片对象不为Null 且 图片的数量>0
    if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
        
        height += _PICTURE_NORMAL_PREVIEW_HEIGHT;
    }
    
    
    //高度要加上一个footer的高度
    height += 18;
    
    //高度最后要加上间隙的高度
    height += 5 + 5 + 5 + 5 + 13;

    return height;
}


// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{
    //获取文字内容
    NSString* content = data[@"content"];
    //获取文字label
    UITextView* contentView = view.subviews[0];
    contentView.text = content ? content : @"";
    
    //由于文字部分高度不定，所以需要在cell重用后向cell中加入文字部分
    //先设置动态内容部分view的高度，再设置其子view的高度，即文字说明的高度
    CGSize size = [contentView.text sizeWithFont:_SYS_FONT_002 constrainedToSize: CGSizeMake(280.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
    contentView.frame = CGRectMake(contentView.frame.origin.x, contentView.frame.origin.y, 280, size.height);

    
    //图片部分
    DynamicPicturesView* picturesView = view.subviews[1];
    //获取图片数据
    NSArray *pictures = data[@"pictures"];

    //有图片 且 图片对象不为Null 且 图片的数量>0
    if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
        [picturesView drawPictures:pictures];
    }
    //否则，就传递nil对象过去，会导致图片视图隐藏
    else {
        [picturesView drawPictures:nil];
    }
    

    //footer
    RTLabel* footerLabel = view.subviews[2];
    footerLabel.text = [NSString stringWithFormat:_L(@"tpl_34"), [data[@"commentCount"] intValue]];
    
    //footer视图部分
//    DynamicFooterView* footerView = view.subviews[2];
//    [footerView setNumbersWithCommented:[data[@"commentCount"] intValue] andSold:0 andRecommended:0];


    //重绘图片视图的位置
    picturesView.frame = CGRectMake(picturesView.frame.origin.x, contentView.frame.origin.y + contentView.frame.size.height + 13, picturesView.frame.size.width, picturesView.frame.size.height);
    //重绘制footer的位置
    footerLabel.frame = CGRectMake(10, picturesView.frame.origin.y + picturesView.frame.size.height + 5, 280, 18);
    //重绘自己视图的|高度|
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, footerLabel.frame.origin.y + footerLabel.frame.size.height);
}


@end
